<?php 
	require('../includes/manager_fns.php');
	$fileName = end(explode('/',$_SERVER['PHP_SELF']));
	$title = '管理员管理'; 
	$act = $_REQUEST['act'];
	$id = $_REQUEST['id'];
	$role_id = 0;
	$title2 = '添加管理员资料';
	switch ($act){
		case 'edit'; //读取表单内容
			$title2 = '编辑管理员资料';
			if (is_numeric($id)){
				$conn = db_connect();
				$sql = 'SELECT * FROM `user` WHERE `user_id` = :id';
				$stmt = $conn -> prepare($sql);
				$stmt -> bindParam(':id',$id);
				$stmt -> execute();
				if (($stmt -> rowCount()) > 0){
					$vo = $stmt -> fetch();
					$user_name = $vo['user_name'];
					$role_id = $vo['role_id'];
				}
			}

			break;
		
		case 'del';//删除
			data_del('user','user_id',$id);
			break;
		
		case 'editup' or 'addup'; //提交数据(添加,修改)
			if($act == 'editup'){
				$title2 = '编辑管理员资料';
			}elseif($act == 'addup'){
				$title2 = '添加管理员资料';
			}
			$user_name = $_POST['user_name'];
			$user_pwd = $_POST['user_pwd'];
			$role_id = $_POST['role_id'];
			
			If(strlen($user_name) < 1 ){
				echo '<script>alert(\'请填写用户名!\');history.back;</script>';
			}elseif($role_id == 0){
				echo '<script>alert(\'请选择角色!\');history.back;</script>';
			}elseif(strlen($user_pwd) < 1){
				echo '<script>alert(\'请填写密码!\');history.back;</script>';
			}else{
				$conn = db_connect();
				if($act == 'editup'){
					$sql = 'UPDATE `user` SET `user_name` = :name,`role_id` = :role_id,`user_pwd` = :pwd WHERE `user_id` = :id';
					$stmt = $conn -> prepare($sql);
					$stmt -> bindParam(':id',$id);
				}elseif($act == 'addup'){
					$sql = 'INSERT INTO `user` (`user_name`,`role_id`,`user_pwd`) VALUES(:name,:role_id,:pwd)';
					$stmt = $conn -> prepare($sql);
				}
				$stmt -> bindParam(':name',$user_name);
				$stmt -> bindParam(':role_id',$role_id);
				$stmt -> bindParam(':pwd',$user_pwd);
				$stmt -> execute();

				echo '<script>alert(\'提交成功!\');location.href = \''.$fileName.'\';</script>';

			}
			break;
	}
	
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $title ?></title>
<script src="js/manager.js" type="text/javascript"></script>
<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	background-color: #F8F9FA;
}
-->
</style>
<link href="images/skin.css" rel="stylesheet" type="text/css" />
</head>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="17" height="29" valign="top" background="images/mail_leftbg.gif"><img src="images/left-top-right.gif" width="17" height="29" /></td>
    <td height="29" valign="top" background="images/content-bg.gif"><table width="100%" height="31" border="0" cellpadding="0" cellspacing="0" class="left_topbg" id="table2">
      <tr>
        <td height="31"><div class="titlebt"><?php echo $title ?></div></td>
      </tr>
    </table></td>
    <td width="16" valign="top" background="images/mail_rightbg.gif"><img src="images/nav-right-bg.gif" width="16" height="29" /></td>
  </tr>
  <tr>
    <td height="71" valign="middle" background="images/mail_leftbg.gif">&nbsp;</td>
    <td valign="top" bgcolor="#F7F8F9"><table width="100%" height="138" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td height="13" valign="top">&nbsp;</td>
      </tr>
      <tr>
        <td valign="top"><table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td class="left_txt">当前位置：<?php echo $title ?></td>
          </tr>
          <tr>
            <td height="20"><table width="100%" height="1" border="0" cellpadding="0" cellspacing="0" bgcolor="#CCCCCC">
              <tr>
                <td></td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td><table width="100%" height="55" border="0" cellpadding="0" cellspacing="0">
              <tr>
                <td width="10%" height="55" valign="middle"><img src="images/title.gif" width="54" height="55"></td>
                <td width="90%" valign="top"><span class="left_txt2">网站后台管理员的管理</span><br>
                          <span class="left_txt2">包括</span><span class="left_txt3">管理员的添加,管理员的修改,管理员的删除</span>。 </span></td>
              </tr>
            </table></td>
          </tr>
          
          
          <tr>
            <td>&nbsp;</td>
          </tr>
        </table>
          <table width="100%" border="0" cellpadding="0" cellspacing="0">
          <form name="addform" action="" method="post"/>
          <?php 
          //判断提交行为
          	if($act == 'edit' || $act == 'editup'){
          		$actValue = 'editup';
          	}else{
          		$actValue = 'addup';
          	}
          ?>
          <input type="hidden" name="act" value="<?php echo $actValue; ?>" />
          <input type="hidden" name="id" value="<?php echo $id ?>" />
            <tr>
              <td colspan="4" align="center" bgcolor="#f2f2f2" class="left_txt"><table width="100%" height="31" border="0" cellpadding="0" cellspacing="0" class="nowtable">
                  <tr>
                    <td class="left_bt2" align="left">&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $title2 ?></td>
                  </tr>
              </table></td>
            </tr>
            <tr bgcolor="#f2f2f2" align="center">
              <td width="27%" class="left_txt2">用户名：</td>
              <td width="27%" class="left_txt" align="left"><input type="text" style="" name="user_name" value="<?php echo $user_name ?>" size="40" class="button01" /></td>
              <td width="24%" class="left_txt2">角色：</td>
              <td width="22%" class="left_txt" align="left">
			  <?php 
              $sql = 'SELECT * FROM `role` ORDER BY `role_id`';
              select_out('选择一个角色',0,$sql,'role_id','role_id','role_name',$role_id)
              ?></td>
            </tr>
            <tr align="center">
              <td class="left_txt2">密码：</td>
              <td class="left_txt" align="left"><input type="text" style="" name="user_pwd" value="<?php echo $user_pwd ?>" size="40" class="button01" /></td>
              <td class="left_txt2"></td>
              <td class="left_txt" align="left">&nbsp;</td>
            </tr>
			<tr bgcolor="">
              <td colspan="4" align="center" class="left_txt"><input type="image" src="images/tj.jpg" name="submit"/></td>
              </tr>
              </form>
          </table>
          <table width="100%" border="0" cellpadding="0" cellspacing="0">
          <form name="addform" action="" method="post"/>
            <tr>
              <td colspan="4" align="center" bgcolor="#f2f2f2" class="left_txt"><table width="100%" height="31" border="0" cellpadding="0" cellspacing="0" class="nowtable">
                  <tr>
                    <td class="left_bt2" align="left">&nbsp;&nbsp;&nbsp;&nbsp;系统模块列表</td>
                  </tr>
              </table></td>
            </tr>
              <tr bgcolor="#f2f2f2">
              <th align="center" class="left_txt2">ID</th>
              <th align="center" class="left_txt2">用户名</th>
              <th align="center" class="left_txt2">角色</th>
              <th align="center" class="left_txt2">操作</th>
              </tr>
            <?php 
            $conn = db_connect();
            $sql = 'SELECT * FROM `user` ORDER BY `user_id`';
            $stmt = $conn -> prepare($sql);
            $stmt -> execute();
            $vo = $stmt -> fetchall();
            $i = 0;
            foreach ($vo as $value){
            	$i += 1;
            	if (fmod($i,2) == 0){
            		$bgcolor = '#f2f2f2';
            	}else{
            		$bgcolor = '';
            	}
            	$sql1 = 'SELECT `role_name` FROM `role` WHERE `role_id` = :id';
            	$stmt1 = $conn -> prepare($sql1);
            	$stmt1 -> bindParam(':id',$value['role_id']);
            	$stmt1 -> execute();
            	$vo1 = $stmt1 -> fetchColumn();
            	
            ?>
            <tr bgcolor="<?php echo $bgcolor ?>">
              <td width="12%" align="center" class="left_txt2"><?php echo $value['user_id'];?></td>
              <td width="42%" align="center" class="left_txt2"><?php echo $value['user_name'];?></td>
              <td width="33%" align="center" class="left_txt"><?php echo $vo1;?></td>
              <td width="13%" align="center" class="left_txt"><a href="?act=edit&id=<?php echo $value['user_id'];?>"><img src="images/edit.gif" border="0"/></a>&nbsp;
              <img src="images/delete.gif" border="0" onclick="delData('<?php echo $fileName; ?>',<?php echo $value['user_id']; ?>)" /></td>
            </tr>
            <?php 
            }
            ?>
            <tr>
              <td colspan="4" align="center" class="left_txt">&nbsp;</td>
            </tr>
              <tr>
                <td>                
              </form>
          </table>
          </td>
      </tr>
    </table></td>
    <td background="images/mail_rightbg.gif">&nbsp;</td>
  </tr>
  <tr>
    <td valign="middle" background="images/mail_leftbg.gif"><img src="images/buttom_left2.gif" width="17" height="17" /></td>
      <td height="17" valign="top" background="images/buttom_bgs.gif"><img src="images/buttom_bgs.gif" width="17" height="17" /></td>
    <td background="images/mail_rightbg.gif"><img src="images/buttom_right2.gif" width="16" height="17" /></td>
  </tr>
</table>
</body>
</html>
